﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using Ext.Net;

public partial class ListEstates : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            this.Store1.DataSource = this.GetDataReader();
            this.Store1.DataBind();
        }                     
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        this.Store1.DataSource = this.GetDataReader();
        this.Store1.DataBind();
    }

    private SqlDataReader GetDataReader()
    {
        SqlConnection myConnection;
        SqlCommand myCommand;
        SqlDataReader myDataReader;

        string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SiyahBayrakConnectionString"].ConnectionString;

        myConnection = new SqlConnection(strConn);
        myConnection.Open();

        myCommand = new SqlCommand("listEstates", myConnection);
        myCommand.CommandType = System.Data.CommandType.StoredProcedure;
        myDataReader = myCommand.ExecuteReader();        

        return myDataReader;
    }

    [DirectMethod]
    public void viewEstate(string command, int estateID)
    {
        if (command == "Details")
        {

            Response.Redirect("EstateDetail.aspx?formID=" + estateID);
        }

        if (command == "Delete")
        {
           
            SqlConnection myConn;
            SqlCommand myComm;
                                    
            string strC = System.Configuration.ConfigurationManager.ConnectionStrings["SiyahBayrakConnectionString"].ConnectionString;            
            myConn = new SqlConnection(strC);
            myConn.Open();

            myComm = new SqlCommand("deleteEstate", myConn);
            myComm.CommandType = System.Data.CommandType.StoredProcedure;
            myComm.Parameters.AddWithValue("estateId", estateID);
            myComm.ExecuteNonQuery();
            myConn.Close();
            
            
            Label1.Text="Selected estate record is deleted. Refresh grid";
        }

        //SqlConnection myConnection2;
        //SqlCommand myCommand2;
        //SqlDataReader myDataReader2;

        //string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SiyahBayrakConnectionString"].ConnectionString;

        //myConnection2 = new SqlConnection(strConn);
        //myConnection2.Open();

        //myCommand2 = new SqlCommand("selectEstate", myConnection2);
        //myCommand2.CommandType = System.Data.CommandType.StoredProcedure;
        //myCommand2.Parameters.AddWithValue("estateId", estateID);

        //myDataReader2 = myCommand2.ExecuteReader();
        //while (myDataReader2.Read())
        //{
        //    idshower.Text = myDataReader2["title"].ToString();
        //}
    }
}